package cn.edu.zafu.camera;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/11/15 0015.
 */

public class OrcTextUtil {

   static String   regEx  =  "[a-zA-Z0-9]";
    static String   regNumber =  "[0-9]";
    static String   regChar  =  "[a-zA-Z]";



    /******
     * 获取识别结果 ，推算识别结果
     * @param orctext
     * @return
     */
    public static String getGussOrcText(String orctext){
        if (orctext==null)
            return null;
        List<String > list=new ArrayList<>();
        String string= formatOrcText(orctext);
        if (orctext==null)
            return null;
        if (checkTextRight(string)){
           if (string.length()>11)
               string=string.substring(0,11);
            return string;
        }

        String prefixChar=HeadCharUtil.getHeadCharByText(string);
        String modeNumer=getCenterNumber(string);
        String lastNumber=getLastNumber(string);
        if (HeadCharUtil.checkHeadChar(prefixChar)){// 头部如果在字典库存在 推算最后一位
            String orcText=getOrcTextByGussLast(prefixChar+modeNumer);
            if (checkTextRight(orcText))
                return orcText;
        }else {
            String orcText=getOrcTextByGussLast(prefixChar+modeNumer);
            if (checkTextRight(orcText)){
                HeadCharUtil.saveHeadCharHeadChar(prefixChar);
                return orcText;
            }

        }

         /*假设 中间 最后一位正确 从字典库取头部 推测*/
        if (prefixChar.length()>=3){
            for (int i = 0; i < prefixChar.length()-3; i++) {// 根据相邻临三位取 头部 加中间 + 最后一位 验证
                String head=prefixChar.substring(i,i+3);
                list= HeadCharUtil.getHeadCharByKey(head);
                if (list!=null&&list.size()>0){
                    for (String shead : list) {
                        String orcText=shead+modeNumer+lastNumber;
                        if (checkTextRight(orcText)){
                            return orcText;
                        }
                    }
                }
            }

            for (int i = 0; i < prefixChar.length()-2; i++) {// 根据相邻临2位取 头部 加中间 + 最后一位 验证
                String head=prefixChar.substring(i,i+2);
                list= HeadCharUtil.getHeadCharByKey(head);
                if (list!=null&&list.size()>0){
                    for (String shead : list) {
                        String orcText=shead+modeNumer+lastNumber;
                        if (checkTextRight(orcText)){
                            return orcText;
                        }
                    }
                }
            }
            for (int i = 0; i < prefixChar.length()-2; i++) {// 任取2位取 头部 加中间 + 最后一位 验证
                for (int j = 1; j < prefixChar.length()-1; j++) {
                    String head=prefixChar.substring(i,i+1)+prefixChar.substring(j,j+1);
                    list= HeadCharUtil.getHeadCharByKey(head);
                    if (list!=null&&list.size()>0){
                        for (String shead : list) {
                            String orcText=shead+modeNumer+lastNumber;
                            if (checkTextRight(orcText)){
                                return orcText;
                            }
                        }
                    }
                }
            }
        }

        list= HeadCharUtil.getHeadCharByKey(prefixChar);// 2位直接取头部 加中间 + 最后一位 验证
        if (list!=null&&list.size()>0){
            for (String shead : list) {
                String orcText=shead+modeNumer+lastNumber;
                if (checkTextRight(orcText)){
                    return orcText;
                }
            }
        }

        if (prefixChar.length()>=3){
            for (int i = 0; i < prefixChar.length()-2; i++) {// 任取2位 从字典库 取 头部 加中间 + 最后一位 验证
                for (int j = 1; j < prefixChar.length()-1; j++) {
                    String head=prefixChar.substring(i,i+1)+prefixChar.substring(j,j+1);
                    list= HeadCharUtil.getHeadCharByKey(head);
                    if (list!=null&&list.size()>0){
                        for (String shead : list) {
                            String orcText=shead+modeNumer+lastNumber;
                            if (checkTextRight(orcText)){
                                return orcText;
                            }
                        }
                    }
                }
            }
        }
        /*假设中部、最后一位正确，从字典取词推测 结束*/
       /* 假设头部，中部正确推测最后一位开始*/

        if (prefixChar.length()>=3){
            for (int i = 0; i < prefixChar.length()-3; i++) {// 根据相邻临三位取 头部 加中间 + 最后一位 验证
                String head=prefixChar.substring(i,i+3);
                list= HeadCharUtil.getHeadCharByKey(head);
                if (list!=null&&list.size()>0){
                    for (String shead : list) {
                        String orcText=getOrcTextByGussLast(shead+modeNumer);
                        if (checkTextRight(orcText)){
                            return orcText;
                        }
                    }
                }
            }

            for (int i = 0; i < prefixChar.length()-2; i++) {// 根据相邻临2位取 头部 加中间 + 最后一位 验证
                String head=prefixChar.substring(i,i+2);
                list= HeadCharUtil.getHeadCharByKey(head);
                if (list!=null&&list.size()>0){
                    for (String shead : list) {
                        String orcText=getOrcTextByGussLast(shead+modeNumer);
                        if (checkTextRight(orcText)){
                            return orcText;
                        }
                    }
                }
            }
        }

        list= HeadCharUtil.getHeadCharByKey(prefixChar);// 2位直接取头部 加中间 + 最后一位 验证
        if (list!=null&&list.size()>0){
            for (String shead : list) {
                String orcText=getOrcTextByGussLast(shead+modeNumer);
                if (checkTextRight(orcText)){
                    return orcText;
                }
            }
        }

        if (prefixChar.length()>=3){
            for (int i = 0; i < prefixChar.length()-2; i++) {// 任取2位 从字典库 取 头部 加中间 + 最后一位 验证
                for (int j = 1; j < prefixChar.length()-1; j++) {
                    String head=prefixChar.substring(i,i+1)+prefixChar.substring(j,j+1);
                    list= HeadCharUtil.getHeadCharByKey(head);
                    if (list!=null&&list.size()>0){
                        for (String shead : list) {
                            String orcText=getOrcTextByGussLast(shead+modeNumer);
                            if (checkTextRight(orcText)){
                                return orcText;
                            }
                        }
                    }
                }
            }
        }

        /*从字典库取头部推测结束*/

        String orcTextGuss=getOrcTextByGussLast(prefixChar+modeNumer);// 假设头部正确推测最后一位
        if (checkTextRight(orcTextGuss))
            return orcTextGuss;


            return orctext+"?";
    }


    public static boolean checkTextRight(String resulst){
        if (resulst==null)
            return false;
        resulst= formatOrcText(resulst);
        if (resulst==null)
            return false;
        resulst=resulst.replace(" ","");
        if (resulst.length()<11)
            return false;
        try
        {
            //获取代码的每位计算值
            String char1 = resulst.substring(0, 1);
            int i1 = changevalue(char1);
            String char2 = resulst.substring(1, 2);
            int i2 = changevalue(char2);
            String char3 = resulst.substring(2, 3);
            int i3 = changevalue(char3);
            String char4 = resulst.substring(3, 4);
            int i4 = changevalue(char4);
            //获取7位箱号的计算值
            int int1 = Integer.parseInt(resulst.substring(4, 5));
            int int2 = Integer.parseInt(resulst.substring(5, 6));
            int int3 = Integer.parseInt(resulst.substring(6, 7));
            int int4 = Integer.parseInt(resulst.substring(7, 8));
            int int5 = Integer.parseInt(resulst.substring(8, 9));
            int int6 = Integer.parseInt(resulst.substring(9, 10));
            int int7 = 0;
            try {
                int7 = Integer.parseInt(resulst.substring(10, 11));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            //计算值求和并对11取余，和第七位箱号对比
            int sum = i1 + 2 * i2 + 4 * i3 + 8 * i4 + 16 * int1 + 32 * int2 + 64 * int3 + 128 * int4 + 256 * int5 + 512 * int6;
            int result = sum % 11;
            if (result == int7 || result - int7 == 10)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    /***
     * 推送最后以为数
     * @param resulst
     * @return
     */
    private static String getOrcTextByGussLast(String resulst)
    {
        if (resulst==null)
            return null;
        resulst= formatOrcText(resulst);
        if (resulst==null)
            return null;
        resulst=resulst.replace(" ","");
        if (resulst.length()<10)
            return null;
        try
        {
            //获取代码的每位计算值
            String char1 = resulst.substring(0, 1);
            int i1 = changevalue(char1);
            String char2 = resulst.substring(1, 2);
            int i2 = changevalue(char2);
            String char3 = resulst.substring(2, 3);
            int i3 = changevalue(char3);
            String char4 = resulst.substring(3, 4);
            int i4 = changevalue(char4);
            //获取7位箱号的计算值
            int int1 = Integer.parseInt(resulst.substring(4, 5));
            int int2 = Integer.parseInt(resulst.substring(5, 6));
            int int3 = Integer.parseInt(resulst.substring(6, 7));
            int int4 = Integer.parseInt(resulst.substring(7, 8));
            int int5 = Integer.parseInt(resulst.substring(8, 9));
            int int6 = Integer.parseInt(resulst.substring(9, 10));

            //计算值求和并对11取余，和第七位箱号对比
            int sum = i1 + 2 * i2 + 4 * i3 + 8 * i4 + 16 * int1 + 32 * int2 + 64 * int3 + 128 * int4 + 256 * int5 + 512 * int6;
            int result = sum % 11;
            if (result==10)
                result=0;
            resulst=resulst+result;
            return resulst;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


    /******
     *  去除空格，非英文数字等
     * @param string
     * @return
     */
    public static String formatOrcText(String string){
        String   regEx  =  "[a-zA-Z0-9]";
        if (string==null)
            return null;
        StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < string.length(); i++) {
                if (Pattern.matches(regEx, string.substring(i, i + 1))) {
                    stringBuffer.append(string.substring(i, i + 1));
                }
            }
            string= stringBuffer.toString().toUpperCase();
       if (string.length()>11) {
           string=string.substring(0,11);
       }
       return string;
    }






    /*******
     * 获取中间数字
     * @param string
     * @return
     */
    public static String getCenterNumber(String string){
        if (string==null)
            return null;
        String df=null;
        String   regEx  =  "[0-9]";
        StringBuffer stringBuffer=new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            if (Pattern.matches(regEx,string.substring(i,i+1)))
            {
                stringBuffer.append(string.substring(i,i+1));
            }
        }
        if (stringBuffer.length()>5)
            df=stringBuffer.substring(0,6).toString();
        return df;
    }

    /*******
     * 获取最后一位数字
     * @param string
     * @return
     */
    public static String getLastNumber(String string){
        if (string==null)
            return null;
        String df=null;
        String   regEx  =  "[0-9]";
        StringBuffer stringBuffer=new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            if (Pattern.matches(regEx,string.substring(i,i+1)))
            {
                stringBuffer.append(string.substring(i,i+1));
            }
        }
        if (stringBuffer.length()>6)
            df=stringBuffer.substring(6,7).toString();
        return df;
    }




    public static int changevalue(String str)
    {
        if (str==null)
            return -1000;
        if (str .equals( "a") ||str .equals("A"))
            return 10;
        else if (str .equals( "b") ||str .equals( "B"))
            return 12;
        else if (str .equals( "c") ||str .equals( "C"))
            return 13;
        else if (str .equals( "d") ||str .equals( "D"))
            return 14;
        else if (str .equals( "e") ||str .equals( "E"))
            return 15;
        else if (str .equals( "f") ||str .equals( "F"))
            return 16;
        else if (str .equals( "g") ||str .equals( "G"))
            return 17;
        else if (str .equals( "h") ||str .equals( "H"))
            return 18;
        else if (str .equals( "i") ||str .equals( "I"))
            return 19;
        else if (str .equals( "j") ||str .equals( "J"))
            return 20;
        else if (str .equals( "k") ||str .equals( "K"))
            return 21;
        else if (str .equals( "l") ||str .equals( "L"))
            return 23;
        else if (str .equals( "m") ||str .equals( "M"))
            return 24;
        else if (str .equals( "n") ||str .equals( "N"))
            return 25;
        else if (str .equals( "o") ||str .equals( "O"))
            return 26;
        else if (str .equals( "p") ||str .equals( "P"))
            return 27;
        else if (str .equals( "q") ||str .equals( "Q"))
            return 28;
        else if (str .equals( "r") ||str .equals( "R"))
            return 29;
        else if (str .equals( "s") ||str .equals( "S"))
            return 30;
        else if (str .equals( "t") ||str .equals( "T"))
            return 31;
        else if (str .equals( "u") ||str .equals( "U"))
            return 32;
        else if (str .equals( "v") ||str .equals( "V"))
            return 34;
        else if (str .equals( "w") ||str .equals( "W"))
            return 35;
        else if (str .equals( "x") ||str .equals( "X"))
            return 36;
        else if (str .equals( "y") ||str .equals( "Y"))
            return 37;
        else if (str .equals( "z") ||str .equals( "Z"))
            return 38;
        else
            return -1000;
    }



}
